﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.ProjectA.Utility
{
    public class ConversionHelper
    {
        /// <summary>
        /// Convert Object To Correct Type Using Reflection
        /// </summary>
        /// <typeparam name="T">T Type - Using Reflection</typeparam>
        /// <param name="objInput">input object will be converted</param>
        /// <returns>output object is converted with correct type</returns>
        public static object ConvertObjToCorrectType<T>(T objInput)
        {
            try
            {
                if (typeof(T) == typeof(System.DateTime))
                {
                    return objInput == null ? new DateTime() : Convert.ToDateTime(objInput);
                }
                else if (typeof(T) == typeof(System.String))
                {
                    return objInput == null ? String.Empty : objInput.ToString();
                }
                else if (typeof(T) == typeof(System.Int32))
                {
                    return objInput == null ? 0 : Convert.ToInt32(objInput.ToString());
                }
                else
                {
                    return default(T);
                }
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }
    }
}
